
package sample;

import java.util.ArrayList;
import java.util.Random;

/**
 * author : ZhihaoDeng
 * created time : 2019/3/17.
 */
public class Commander {
    private String name;
    private static int maxNumOfCommander = 10;
    private static ArrayList<Commander> list = new ArrayList<>(maxNumOfCommander);
    private static int countOfCommand = 0;

    /**
     * 私有化command构造方法
     */
    private Commander() {
        this.name = "commander" + countOfCommand++;
    }

    /**
     * 初始化十个commander
     */
    static {
        for (int i = 0; i < 10; i++) {
            list.add(new Commander());
        }
    }

    public static Commander getInstance() {
        Random random = new Random();
        countOfCommand = random.nextInt(maxNumOfCommander);
        return list.get(countOfCommand);
    }

    public String getName() {
        return this.name;
    }
}
